package com.mina.app;

import com.mina.handler.MinaServerHandler;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.net.InetSocketAddress;

/**
 * 服务端
 *
 * @author sam
 */
public class MinaServer {

    public static void main(String[] args) {

        // nio service socket
        SocketAcceptor acceptor = new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chainBuilder = acceptor.getFilterChain();

        // settings
        chainBuilder.addLast("objectFilter", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        acceptor.setHandler(new MinaServerHandler());
        int port = 9999;

        // start
        try {
            acceptor.bind(new InetSocketAddress(port));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("mina server started -> port:" + port);
    }
}
